// components/bottom-bar/bottom-bar.js
Component({
  properties: {
    current: {
      type: String,
      value: "index",
    },
  },

  data: {
    tabs: [
      {
        id: 1,
        text: "首页",
        path: "/pages/index/index",
        icon: "/images/tab-home.png",
        activeIcon: "/images/tab-home-active.png",
      },
      {
        id: 2,
        text: "发布",
        path: "/pages/post/post",
        icon: "/images/tab-post.png",
        activeIcon: "/images/tab-post-active.png",
      },
      {
        id: 3,
        text: "附近",
        path: "/pages/nearby/nearby",
        icon: "/images/tab-nearby.png",
        activeIcon: "/images/tab-nearby-active.png",
      },
      {
        id: 4,
        text: "我的",
        path: "/pages/profile/profile",
        icon: "/images/tab-profile.png",
        activeIcon: "/images/tab-profile-active.png",
      },
    ],
  },

  lifetimes: {
    attached() {
      this.updateSelectedTab();
    },
  },

  observers: {
    current: function (newVal) {
      this.updateSelectedTab();
    },
  },

  methods: {
    // 更新选中的标签
    updateSelectedTab() {
      const tabs = this.data.tabs.map((tab) => {
        tab.selected = tab.path.includes(this.data.current);
        return tab;
      });
      this.setData({ tabs });
    },

    // 切换标签
    switchTab(e) {
      const path = e.currentTarget.dataset.path;
      wx.switchTab({
        url: path,
      });
    },
  },
});
